package com.wenx.v3system.modular.cloud.controller;

import com.wenx.v3core.response.R;
import com.wenx.v3system.modular.cloud.domain.dto.UserAuthResponseDto;
import com.wenx.v3system.modular.cloud.domain.dto.UserLoginDto;
import com.wenx.v3system.modular.cloud.domain.dto.UserRegisterDto;
import com.wenx.v3system.modular.cloud.service.SysAuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 系统认证控制器
 * 负责用户注册、登录、登出等认证相关功能
 * 遵循项目框架规范，移除event模式，使用直接服务调用
 * 
 * @author wenx
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "系统认证管理", description = "用户注册、登录、登出等认证功能")
@RequestMapping("/api/v3/sys/auth")
public class SysAuthController {

    private final SysAuthService authService;

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "新用户注册接口")
    public R register(@Valid @RequestBody UserRegisterDto registerDto) {
        log.info("用户注册请求: username={}", registerDto.getUsername());
        
        UserAuthResponseDto response = authService.register(registerDto);
        return R.ok("注册成功", response);
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口")
    public R login(@Valid @RequestBody UserLoginDto loginDto) {
        log.info("用户登录请求: username={}", loginDto.getUsername());
        
        UserAuthResponseDto response = authService.login(loginDto);
        return R.ok("登录成功", response);
    }

    @PostMapping("/logout")
    @Operation(summary = "用户登出", description = "用户登出接口")
    public R logout(@Parameter(description = "用户ID") @RequestParam Long userId) {
        log.info("用户登出请求: userId={}", userId);
        
        authService.logout(userId);
        return R.ok("登出成功");
    }

    @PostMapping("/refresh")
    @Operation(summary = "刷新令牌", description = "使用刷新令牌获取新的访问令牌")
    public R refreshToken(@Parameter(description = "刷新令牌") @RequestParam String refreshToken) {
        log.info("刷新令牌请求: refreshToken={}", refreshToken);

        UserAuthResponseDto response = authService.refreshToken(refreshToken);
        return R.ok("令牌刷新成功", response);
    }

    @GetMapping("/validate")
    @Operation(summary = "验证令牌", description = "验证访问令牌的有效性")
    public R validateToken(@Parameter(description = "访问令牌") @RequestParam String token) {
        log.debug("验证令牌请求: token={}", token);

        Map<String, Object> validationResult = authService.validateToken(token);
        return R.ok("令牌验证完成", validationResult);
    }

    @GetMapping("/current-user")
    @Operation(summary = "获取当前用户", description = "获取当前登录用户信息")
    public R getCurrentUser() {
        log.debug("获取当前用户信息");
        
        UserAuthResponseDto response = authService.getCurrentUser();
        return R.ok("获取成功", response);
    }



    @GetMapping("/check/username")
    @Operation(summary = "检查用户名", description = "检查用户名是否已存在")
    public R checkUsername(@Parameter(description = "用户名") @RequestParam String username) {
        log.debug("检查用户名: username={}", username);
        
        boolean exists = authService.checkUsernameExists(username);
        return R.ok("检查完成", Map.of(
            "username", username,
            "exists", exists,
            "available", !exists
        ));
    }

    @GetMapping("/check/email")
    @Operation(summary = "检查邮箱", description = "检查邮箱是否已存在")
    public R checkEmail(@Parameter(description = "邮箱") @RequestParam String email) {
        log.debug("检查邮箱: email={}", email);
        
        boolean exists = authService.checkEmailExists(email);
        return R.ok("检查完成", Map.of(
            "email", email,
            "exists", exists,
            "available", !exists
        ));
    }

    @GetMapping("/check/phone")
    @Operation(summary = "检查手机号", description = "检查手机号是否已存在")
    public R checkPhone(@Parameter(description = "手机号") @RequestParam String phone) {
        log.debug("检查手机号: phone={}", phone);
        
        boolean exists = authService.checkPhoneExists(phone);
        return R.ok("检查完成", Map.of(
            "phone", phone,
            "exists", exists,
            "available", !exists
        ));
    }
}